package com.example.mysaat;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.mysaat.handle_utils.AbstarcParse;
import com.example.mysaat.handle_utils.ManagerActivityInterface;
import com.example.mysaat.handle_utils.ManagerActivityUtil;
import com.example.mysaat.handle_utils.ManagerActivityUtilProxy;
import com.example.mysaat.handle_utils.XMLDatabase;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ListAdapter adapter;
    private AbstarcParse parser=null;
    private String[]datas = null;
    public static Context context;
    private static final String TAG = "MainActivity";
    private ManagerActivityUtilProxy<ManagerActivityInterface<Activity>> a = null;
    //临时的一个数据存储
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = MainActivity.this;
        parser = new XMLDatabase();
        //在main_activity 里面会有一个适配器
        init();
        initContent();
        //添加点击对象的监听事件
        listView.setOnItemClickListener(new MyMainOnItemClick());
    }

    private void init (){
        listView = findViewById(R.id.main_list_view);
    }

    private void initContent(){
        datas = (String[])parser.getParseData("saat_root_direction",R.xml.saat_root_direction);
        adapter = new MyArrayAdapter(this,android.R.layout.simple_list_item_1,datas);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(a==null)
        a = new ManagerActivityUtilProxy();
        a.getProxytyInstance(new ManagerActivityUtil()).shieldMenuKey(MainActivity.this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        a=null;
    }

    @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch(keyCode){
                case KeyEvent.KEYCODE_VOLUME_UP:
                    Log.d(TAG, "KeyEvent.KEYCODE_MENU");
                    Toast.makeText(this, "KeyEvent.KEYCODE_MENU", Toast.LENGTH_LONG);
                    break;
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    Log.d(TAG, "KeyEvent.KEYCODE_HOME");
                    finish();
                    Toast.makeText(this, "KeyEvent.KEYCODE_HOME", Toast.LENGTH_LONG);
                    break;
                case KeyEvent.KEYCODE_BACK:
                    return true;
                default:
                    Log.d(TAG, "没有匹配的");

                    Toast.makeText(this, "KeyEvent.没有匹配的", Toast.LENGTH_LONG);
            }
            return super.onKeyDown(keyCode, event);
        }
}
